package com.turing.usdtpay.mvp.dialog;

import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseDialog;
import com.turing.usdtpay.common.StateContants;
import com.turing.usdtpay.utils.ScreenUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/12 10:31
 * @描述：
 **/
public class ConfirmMoneyDialog extends BaseDialog<ConfirmMoneyDialog> {
    private TextView mCancel,mConfirm;
    private EditText mInput;
    private LinearLayout mConfirmLayout,mRepeatConfirmLayout;
    private boolean isRepeat = false;
    private double mMoney = 0;
    private ConfirmListener listener;
    public ConfirmMoneyDialog(Context context) {
        super(context);
    }

    public ConfirmMoneyDialog(Context context, boolean isPopupStyle) {
        super(context, isPopupStyle);
    }

    public void setListener(ConfirmListener listener) {
        this.listener = listener;
    }

    public ConfirmMoneyDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public View onCreateView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_confirm_money, null);
        mCancel = (TextView) view.findViewById(R.id.tv_cancel);
        mConfirm = (TextView) view.findViewById(R.id.tv_confirm);
        mInput = (EditText) view.findViewById(R.id.et_input);
        mConfirmLayout = (LinearLayout) view.findViewById(R.id.confirm_layout);
        mRepeatConfirmLayout = (LinearLayout) view.findViewById(R.id.repeat_confirm_layout);
        return view;
    }

    @Override
    public void setUiBeforeShow() {
        setCanceledOnTouchOutside(false);
        mCancel.setOnClickListener(this);
        mConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mCancel.getId()){
            dismiss();
        }else if (v.getId() == mConfirm.getId()){
            if (TextUtils.isEmpty(mInput.getText())){
                showShortToast("请输入金额");
            }else {
                if (!isRepeat){
                    try {
                        if (Double.valueOf(mInput.getText().toString()) == mMoney){
                            isRepeat = true;
                            mConfirmLayout.setVisibility(View.GONE);
                            mRepeatConfirmLayout.setVisibility(View.VISIBLE);
                        }else {
                            showShortToast("收到的金额与应收的金额不一致");
                        }
                    }catch (Exception e){
                        e.getMessage();
                        showShortToast("收到的金额与应收的金额不一致");
                    }
                }else {
                    listener.onConfirm();
                    dismiss();
                }

            }
        }
    }

    public void setMoney(double money){
        mMoney = money;
    }

    public interface ConfirmListener{
        public void onConfirm();
    }

    @Override
    public void show() {
        super.show();
        int mOrientation = mActivity.getResources().getConfiguration().orientation;
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        if (Configuration.ORIENTATION_PORTRAIT == mOrientation) {
            layoutParams.width = (int) (ScreenUtils.getScreenWidth(mContext) * StateContants.FLOW_WINDOW_PORTRAIT_WIDTH_SCALE);
//            layoutParams.height = (int) (ScreenUtils.getScreenHeight(mContext) * SDKConstants.FLOW_WINDOW_PORTRAIT_HEIGHT_SCALE);
        }else {
            layoutParams.width = (int) (ScreenUtils.getScreenWidth(mContext)* StateContants.FLOW_WINDOW_PORTRAIT_WIDTH_SCALE);
//            layoutParams.height = (int) (ScreenUtils.getScreenHeight(mContext) * SDKConstants.FLOW_WINDOW_LANDSCAPE_HEIGHT_SCALE);
        }
        getWindow().setAttributes(layoutParams);
    }
}
